package com.itcoon.cloud.framework.i18n.config;

import com.itcoon.cloud.framework.i18n.bundle.BaseBundle;
import com.itcoon.cloud.framework.i18n.bundle.Bundle;
import com.itcoon.cloud.framework.i18n.bundle.ExBundle;
import com.itcoon.cloud.framework.i18n.config.properties.BundleMessageProperties;
import com.itcoon.cloud.framework.i18n.registry.BundleRegistry;
import com.itcoon.cloud.framework.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.Map;

@AutoConfiguration
@AutoConfigureAfter({SpringUtils.class})
@EnableConfigurationProperties(BundleMessageProperties.class)
public class I18nConfiguration {

    private static final Logger log = LoggerFactory.getLogger(I18nConfiguration.class);

    public I18nConfiguration(BundleMessageProperties messageProperties) {
        BundleRegistry bundleRegistry = new BundleRegistry();
        Map<String, BundleConfigurer> bundleConfigMap = SpringUtils.getBeansMap(BundleConfigurer.class);
        bundleConfigMap.values().forEach(config->config.addBundles(bundleRegistry));
        List<Bundle> bundles = bundleRegistry.getBundles();
        bundles.addAll(messageProperties.getBaseBundles());
        bundles.addAll(messageProperties.getExBundles());
        log.info("============================================= I18n Message Source Start=====================================================");
        for (Bundle bundle : bundles) {
            MessageSource messageSource = messageSource(bundle);
            String registerKey = bundle instanceof ExBundle ? ((ExBundle<?, ?>) bundle).getEx().getName() : ((BaseBundle)bundle).getRegisterKey();
            SpringUtils.registerSingleton(registerKey, messageSource);
        }
        log.info("============================================= I18n Message Source End =====================================================");
    }

    private MessageSource messageSource(Bundle bundle) {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
        if (StringUtils.hasText(bundle.getBaseName())) {
            messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(bundle.getBaseName())));
        }

        messageSource.setFallbackToSystemLocale(bundle.isFallbackToSystemLocale());
        Duration cacheDuration = bundle.getCacheDuration();
        if (cacheDuration != null) {
            messageSource.setCacheMillis(cacheDuration.toMillis());
        }
        messageSource.setAlwaysUseMessageFormat(bundle.isAlwaysUseMessageFormat());
        messageSource.setUseCodeAsDefaultMessage(bundle.isUseCodeAsDefaultMessage());
        return messageSource;
    }




}
